package com.siwei.mes.service.system.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.siwei.mes.entity.system.ProjectArea;
import com.siwei.mes.mapper.system.HomeMapper;
import com.siwei.mes.mapper.system.ProjectAreaMapper;
import com.siwei.mes.service.system.HomeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static cn.dev33.satoken.SaManager.log;

/**
 * @program: mes-platform
 * @description:
 * @author: yangke
 * @create: 2024-01-01 17:59
 **/
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private HomeMapper homeMapper;
    @Resource
    private ProjectAreaMapper projectAreaMapper;

    @Override
    public Map homeAnalysis() {
        String userId = StpUtil.getLoginIdAsString();
        Map<String, Object> result = new HashMap();
        /*试验分析*/
        try {
            result.put("experimentAnalysis", homeMapper.experimentAnalysisAll());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("试验分析异常>>>>>>>>>>>", e);
        }
        /*工单分析*/
        try {
            /**判断是否为巡查组长，巡查组长查询所有区域划分里的用户数据*/
            List<ProjectArea> list = projectAreaMapper.getList(new ProjectArea().setHeadmanId(userId));
            if (CollectionUtils.isNotEmpty(list)) {
                List<String> userIds = new ArrayList(Arrays.asList(list.get(0).getCustomerServiceId().split(",")));
                userIds.add(userId);
                Map<String, Object> params = new HashMap();
                params.put("userIds", userIds);
                params.put("userId", userId);
                result.put("orderAnalysis", homeMapper.orderAnalysisByUserList(params));
            } else {
                result.put("orderAnalysis", homeMapper.orderAnalysis(userId));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("工单分析异常>>>>>>>>>>>", e);
        }
        /*强度波动分析*/
        try {
            result.put("intensityFluctuationsAnalysis", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("强度波动分析异常>>>>>>>>>>>", e);
        }
        /*采购预估*/
        try {
            result.put("purchaserPredict", homeMapper.purchaserPredict());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("采购预估异常>>>>>>>>>>>", e);
        }
        /*工单趋势分析*/
        try {
            result.put("orderTrendAnalysis", homeMapper.orderTrendAnalysis());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("工单趋势分析异常>>>>>>>>>>>", e);
        }
        /*工单类型分析*/
        try {
            result.put("workOrderTypeAnalysis", homeMapper.workOrderTypeAnalysis());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("工单类型分析异常>>>>>>>>>>>", e);
        }
        return result;
    }

    @Override
    public Map experimentAnalysis() {
        /**String userId = StpUtil.getLoginIdAsString();*/
        return homeMapper.experimentAnalysisAll();
    }

    @Override
    public Map orderAnalysis() {
        String userId = StpUtil.getLoginIdAsString();
        /**判断是否为巡查组长，巡查组长查询所有区域划分里的用户数据*/
        List<ProjectArea> list = projectAreaMapper.getList(new ProjectArea().setHeadmanId(userId));
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> userIds = Arrays.asList(list.get(0).getCustomerServiceId().split(","));
            userIds.add(userId);
            Map<String, Object> params = new HashMap();
            params.put("userIds", userIds);
            params.put("userId", userId);
            return homeMapper.orderAnalysisByUserList(params);
        }
        return homeMapper.orderAnalysis(userId);
    }

    @Override
    public List<Map> intensityFluctuationsAnalysis() {
        return null;
    }

    @Override
    public List<Map> purchaserPredict() {
        return null;
    }

    @Override
    public List<Map> orderTrendAnalysis() {
        return homeMapper.orderTrendAnalysis();
    }

    @Override
    public List<Map> workOrderTypeAnalysis() {
        return homeMapper.workOrderTypeAnalysis();
    }
}
